import {
  mockLogin,
  mockGetUserInfo,
  mockLogout,
  mockChangePassword,
  mockGetUserById,

} from '@/mock/user'
import request from '@/utils/request'
import type { UserInfo, UserQuery, PageResult, ApiResponse, ResetPasswordParams, AssignRoleParams, RoleInfo } from '@/types/user.type'

// 用户登录
export function login(data: { username: string; password: string }) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const res = mockLogin(data.username, data.password)
      if (res.code === 0) {
        resolve(res.data)
      } else {
        reject(new Error(res.message))
      }
    }, 500)
  })
}

// 获取用户信息
export function getUserInfo(token: string) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const res = mockGetUserInfo(token)
      if (res.code === 0) {
        resolve(res.data)
      } else {
        reject(new Error(res.message))
      }
    }, 500)
  })
}

// 用户登出
export function logout(token: string) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const res = mockLogout(token)
      if (res.code === 0) {
        resolve(res.data)
      } else {
        reject(new Error(res.message))
      }
    }, 500)
  })
}

// 修改密码
export function changePassword(data: {
  token: string
  oldPassword: string
  newPassword: string
}) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const res = mockChangePassword(data.token, data.oldPassword, data.newPassword)
      if (res.code === 0) {
        resolve(res.data)
      } else {
        reject(new Error(res.message))
      }
    }, 500)
  })
}

// 获取用户列表
export function getUserList(params: UserQuery): Promise<PageResult<UserInfo>> {
  return request({
    url: '/api/system/users/page',
    method: 'get',
    params: {
      current: params.current || 1,
      size: params.size || 10,
      orderField: params.orderField,
      orderType: params.orderType,
      username: params.username,
      realName: params.realName,
      phone: params.phone,
      email: params.email,
      status: params.status,
      deptId: params.deptId
    }
  })
}

// 获取用户详情
export function getUserById(id: number): Promise<ApiResponse<UserInfo>> {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const res = mockGetUserById(id)
      if (res.code === 0) {
        resolve(res)
      } else {
        reject(new Error(res.msg))
      }
    }, 500)
  })
}

// 创建用户
export function createUser(data: Omit<UserInfo, 'id'>): Promise<ApiResponse<void>> {
  return request({
    url: '/api/system/users',
    method: 'post',
    data
  })
}

// 更新用户
export function updateUser(data: UserInfo): Promise<ApiResponse<void>> {
  return request({
    url: '/api/system/users',
    method: 'put',
    data
  })
}

// 删除用户
export const deleteUser = (ids: number[]) => {
  return request.delete(`/api/system/users/${ids.join(',')}`)
}

// 重置密码
export function resetPassword(data: { userId: number; password: string }): Promise<ApiResponse<void>> {
  return request({
    url: `/api/system/users/password/reset/${data.userId}`,
    method: 'put',
    data: { password: data.password }
  })
}

// 获取角色列表
export function getRoleList(): Promise<RoleInfo[]> {
  return request({
    url: '/api/system/roles/list',
    method: 'get'
  })
} 